﻿using System;
using System.Globalization;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;

using zevenseas.LifeCycleManagement.Utilities;
using zevenseas.LifeCycleManagement.TimerJobs;
using Microsoft.SharePoint.WebPartPages;


namespace zevenseas.LifeCycleManagement.ApplicationPages
{
    public class Settings : Microsoft.SharePoint.WebControls.LayoutsPageBase
    {
        private SPWeb web = SPContext.Current.Web;
        private ErrorHandling errorHandling;
                
        protected WebApplicationSelector webApplicationSelector;

        protected InputFormTextBox txtTimePeriodUnused;
        protected InputFormTextBox txtTimePeriodLock;
        protected InputFormTextBox txtTimePeriodMail;
        protected InputFormTextBox txtMailContent;
        protected InputFormTextBox txtMailSubject;
              
        protected InputFormCheckBox cbxDeleteSite;
        protected InputFormCheckBox cbxLockSite;
        protected InputFormCheckBox cbxMailOwner;
        protected InputFormCheckBox cbxContentDate;
        protected InputFormCheckBox cbxSecurityDate;
        protected InputFormCheckBox cbxUsageDate;

        protected Label output;

        protected Button btnOk;

        protected CheckBox uxIncludeWebs;
        

        public Settings()
        {
            errorHandling = new ErrorHandling();
            errorHandling.productName = "Settings";
            errorHandling.exeName = "zevenseas.LifeCycleManagement.ApplicationPages";
        }


        protected void OnClickOK(Object Sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Web;

            

            try
            {
                SPWebApplication webApplication = webApplicationSelector.CurrentItem;

                SiteLifeCycleSettings settings = webApplication.GetChild<SiteLifeCycleSettings>("SiteLifeCycleSettings");
                
                // If no settings previously created, create them now.    
                if (settings == null)
                {
                    SPPersistedObject parent = webApplication;
                    settings = new SiteLifeCycleSettings("SiteLifeCycleSettings", parent, Guid.NewGuid());                        
                    settings.Update();
                }

                //Setting the LCMStatus enum to the least 'worse' action
                settings.deleteSite = cbxDeleteSite.Checked; if (settings.deleteSite) { settings.status = LCMStatus.Delete; }
                settings.lockSite = cbxLockSite.Checked; if (settings.lockSite) { settings.status = LCMStatus.Lock; }
                settings.mailOwner = cbxMailOwner.Checked; if (settings.mailOwner) { settings.status = LCMStatus.Mail; }

                settings.unusedSpan = int.Parse(txtTimePeriodUnused.Text.ToString());
                settings.emailSpan = int.Parse(txtTimePeriodMail.Text.ToString());
                settings.lockSpan = int.Parse(txtTimePeriodLock.Text.ToString());
                //settings.deleteSpan = int.Parse(txtTimePeriodDelete.Text.ToString());

                settings.emailBody = txtMailContent.Text.ToString();
                settings.emailSubject = txtMailSubject.Text.ToString();

                settings.lastContentModifiedDate = cbxContentDate.Checked;
                settings.lastSecurityModifiedDate = cbxSecurityDate.Checked;
                settings.lastUsageDate = cbxUsageDate.Checked;

                settings.includeWebs = uxIncludeWebs.Checked;

                settings.Update();
            }

            catch (Exception setError)
            {
                errorHandling.LogMessage(setError.Message.ToString(), setError);
                Utilities.Utilities.SetWebProperty("false", "Configured", web.Properties);
            }


            this.Page.Response.Redirect("/_admin/applications.aspx");
        }

        protected void OnClickCancel(Object Sender, EventArgs e)
        {
            this.Page.Response.Redirect("/_admin/applications.aspx");
        }

        protected override void OnLoadComplete(EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                try
                {
                    SPWebApplication webApplication = webApplicationSelector.CurrentItem;

                    if (!Utilities.Utilities.IsEmailConfigured())
                    {                   
                        cbxMailOwner.Enabled = false;
                        txtTimePeriodMail.Enabled = false;
                        txtMailContent.Enabled = false;
                        txtMailSubject.Enabled = false;
                        cbxMailOwner.LabelText = SPEncode.HtmlDecode("Please configure the outgoing E-mail settings");
                    }

                    //if (Utilities.Utilities.GetWebProperty("Configured", web.Properties) == "false")
                    //{
                        if (webApplication != null)
                        {
                            SiteLifeCycleSettings settings = webApplication.GetChild<SiteLifeCycleSettings>("SiteLifeCycleSettings");

                            if (settings != null)
                            {
                                cbxDeleteSite.Checked = settings.deleteSite;
                                cbxLockSite.Checked = settings.lockSite;


                                txtTimePeriodUnused.Text = settings.unusedSpan.ToString();

                                txtTimePeriodLock.Text = settings.lockSpan.ToString();
                                //txtTimePeriodDelete.Text = settings.deleteSpan.ToString();

                                if (Utilities.Utilities.IsEmailConfigured())
                                {
                                    cbxMailOwner.Checked = settings.mailOwner;
                                    txtTimePeriodMail.Text = settings.emailSpan.ToString();
                                    txtMailContent.Text = settings.emailBody.ToString();
                                    txtMailSubject.Text = settings.emailSubject.ToString();
                                }

                                cbxContentDate.Checked = settings.lastContentModifiedDate;
                                cbxSecurityDate.Checked = settings.lastSecurityModifiedDate;
                                cbxUsageDate.Checked = settings.lastUsageDate;

                                uxIncludeWebs.Checked = settings.includeWebs;
                            }
                        }
                    //}
                }
                catch (Exception configGetError)
                {
                    errorHandling.LogMessage(configGetError.Message.ToString(), configGetError);
                }
            }

            base.OnLoadComplete(e);
        }
    }  
}






